﻿using System;
using msBsc.AppLogic.DataAlc;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfShopInfoSet : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BoundBankList();
                showUserInfo();
            }
        }

        private void BoundBankList()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_BANK ";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplBANK_CDE", ds, "BANK_NME", "BANK_CDE", "", "", true);
            }
        }

        private void showUserInfo()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpScorePub objpub = new CBizShpScorePub())
            {
                DataSet ds = objpub.DataObj.QueryBySQL(@"
    SELECT 
    COMM_TYPE_TXT=CASE WHEN A.COMM_TYPE_CID='1' THEN '个人' ELSE '企业' END
    ,A.USER_NME
    ,A.CONTACTS_EMAIL
    ,A.COMM_TENANT_NME
    ,A.ZONE_DESC
    ,A.DEPOSIT_AMT
    ,A.DEPOSIT_TRADING_NO
    ,A.COMPANY_NME_CN
    ,A.COMPANY_NME_EN
    ,A.LEGAL_OWNER
    ,A.REGISTERED_CAPITAL
    ,A.REGISTERED_ADDRESS
    ,REGISTERED_DT=CONVERT(varchar(10), A.REGISTERED_DT, 23)
    ,A.BUSINESS_LICENSE_NO
    ,A.TAX_REGISTRATION_NO
    ,A.CONTACTS
    ,A.CONTACTS_ADDRESS
    ,A.CONTACTS_TEL
    ,A.CONTACTS_PHONE
    ,A.ALIPAY_MAIL 
    ,A.BANK_CDE
    ,A.BANK_ACCOUNT_NO
    ,A.BANK_ACCOUNT_NME 
    FROM SHP_COMMERCIAL_TENANT_INFO  A   WHERE A.COMM_TENANT_ID='" + this.oUserInfo.OperDealerId.Split(',')[0] + @"'
    ");
                this.AddPageRetRow("divInputInfo", ds.Tables[0].Rows[0]);
                this.SetContrlValue("dplBANK_CDE", ds.Tables[0].Rows[0]["BANK_CDE"].ToString());
                if (ds.Tables[0].Rows[0]["COMM_TYPE_TXT"].ToString() == "企业")
                {
                    this.ExecJScript("$('#divCompany').show();");
                }
                if (ds.Tables[0].Rows[0]["DEPOSIT_AMT"].ToString() == string.Empty || ds.Tables[0].Rows[0]["DEPOSIT_AMT"].ToString()=="0.00")
                {
                    this.ExecJScript("$('#spndoPayDeposit').show();");

                    using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo objtrakcharge = new CBizShpCommercialTenantInfo())
                    {
                        string strpaydepositamt = msBsc.AppLogic.Cfg.ApplicationConst.Const.getConstSetVal<decimal>("_SHOP_DEPOSIT_AMT").ToString();
                        string strtradno = ds.Tables[0].Rows[0]["DEPOSIT_TRADING_NO"].ToString();
                        if (strtradno != string.Empty)
                        {
                            DataSet dsLog = objtrakcharge.DataObj.QueryBySQL("SELECT TRADING_AMT FROM SHP_CHARGE_TRADING_LOG A WHERE A.PAY_DIR='支出' AND TRADING_NO = '" + strtradno + "'");
                            if (dsLog.Tables[0].Rows.Count <= 0)
                            {
                                strtradno = "";
                            }
                            else
                            {
                                decimal dectm = 0M;
                                for (int i = 0; i < dsLog.Tables[0].Rows.Count; i++)
                                {
                                    dectm += (decimal)dsLog.Tables[0].Rows[i]["TRADING_AMT"];
                                }
                                strpaydepositamt = dectm.ToString();
                            }
                        }

                        if (strtradno == string.Empty)
                        {
                            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                            transactionOption.Timeout = new TimeSpan(0, 10, 0);
                            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                            {
                                DataSet dsshop=objtrakcharge.SelectById(this.oUserInfo.OperDealerId.Split(',')[0]);
                                if (dsshop.Tables[0].Rows.Count > 0)
                                {
                                    strtradno = objtrakcharge.AddDepositAmtPayTradingLog(dsshop.Tables[0].Rows[0]);
                                    objtrakcharge.Update(dsshop);
                                    ts.Complete();
                                }
                            }

                        }

                        if (strtradno != string.Empty)
                        {
                            this.ExecJScript("$('#spndoPayDeposit').html('（ 应缴金额：<em style=\"color:#c40000;\">" + strpaydepositamt + "</em>元 <a class=\"  \" href=\"/c_PayCashier.htm?ID=" + strtradno + "\"  target=\"_blank\">立即付款</a>）');");
                        } 
                    }
                }

                msBsc.AppLogic.Cfg.ApplicationConst.Const.getConstSetVal<decimal>("_SHOP_DEPOSIT_AMT");

                //if (ds.Tables[0].Rows[0]["LOGO_IMG_URL"].ToString() != string.Empty)
                //{
                //    this.ExecJScript("$('#logo_img_selected').html('<img alt=\"\" src=\"" + ds.Tables[0].Rows[0]["LOGO_IMG_URL"].ToString() + "\" for=\"" + ds.Tables[0].Rows[0]["LOGO_IMG_URL"].ToString() + "\" width=\"100%\" height=\"100%\">');");
                //} 
                
                //SetMode("VIEW");
            }
        }

        //修改页面状态
        private void SetMode(string strMode)
        {
            EditMode = strMode; 
            switch (strMode)
            {
                case "EDIT":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    SetPanalEditMode("divInputInfo", "EDIT");
                    break; 
                case "VIEW":
                    EnabledLinkBtn("btnEdit", true);
                    EnabledLinkBtn("btnSave", false);
                    SetPanalEditMode("divInputInfo", "VIEW");
                    break;
            }
        }
        private void doSave()
        {

            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                //using (msBsc.AppLogic.DataAlc.CBizSysUser objss = new CBizSysUser())
                //{
                //    DataSet ds1 = objss.SelectById(this.oUserInfo.Operator);
                //    if (ds1.Tables[0].Rows.Count > 0)
                //    {
                //        ds1.Tables[0].Rows[0]["TEL"] = this.GetPostHtValue("TEL");
                //        ds1.Tables[0].Rows[0]["QQ_NO"] = this.GetPostHtValue("QQ_NO");
                //        //ds1.Tables[0].Rows[0]["PROMOTION_NME"] = this.GetPostHtValue("PROMOTION_NME");
                //        objss.Update(ds1);
                //    }
                //}
                using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo objss = new CBizShpCommercialTenantInfo())
                {
                    DataSet ds1 = objss.SelectById(this.oUserInfo.OperDealerId.Split(',')[0]);
                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        ds1.Tables[0].Rows[0]["CONTACTS"] = this.GetPostHtValue("CONTACTS");
                        ds1.Tables[0].Rows[0]["CONTACTS_ADDRESS"] = this.GetPostHtValue("CONTACTS_ADDRESS");
                        ds1.Tables[0].Rows[0]["CONTACTS_TEL"] = this.GetPostHtValue("CONTACTS_TEL");
                        ds1.Tables[0].Rows[0]["CONTACTS_PHONE"] = this.GetPostHtValue("CONTACTS_PHONE");
                        ds1.Tables[0].Rows[0]["ALIPAY_MAIL"] = this.GetPostHtValue("ALIPAY_MAIL");
                        ds1.Tables[0].Rows[0]["BANK_CDE"] = this.GetPostHtValue("BANK_CDE");
                        ds1.Tables[0].Rows[0]["BANK_ACCOUNT_NO"] = this.GetPostHtValue("BANK_ACCOUNT_NO");
                        ds1.Tables[0].Rows[0]["BANK_ACCOUNT_NME"] = this.GetPostHtValue("BANK_ACCOUNT_NME");
                        //ds1.Tables[0].Rows[0]["LOGO_IMG_URL"] = this.GetPostHtValue("LOGO_IMG_URL");

                        //if (this.GetPostHtValue("LOGO_IMG_URL") != string.Empty && ds1.Tables[0].Rows[0]["LOGO_IMG_URL"].ToString() != this.GetPostHtValue("LOGO_IMG_URL"))
                        //{
                        //    using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
                        //    {
                        //        //保存身份证A面
                        //        ds1.Tables[0].Rows[0]["LOGO_IMG_URL"] = oCBizSysFile.SaveImgToServer(""
                        //            , this.oUserInfo.Operator
                        //            , "0"
                        //            , "LOGO"
                        //            , ""
                        //            , this.GetPostHtValue("LOGO_IMG_URL")
                        //            , ""
                        //            , true);
                        //    }
                        //}

                        objss.Update(ds1);
                    }
                }
                ts.Complete();
            }

            this.ExecJScript("layer.msg('已成功保存！',1,{type:1,shade:false});");
        }

        public override void DoAction()
        {
            if (this.strAction == "btnSave")
            {
                doSave();
                //SetMode("VIEW");
            }
            else if (strAction == "btnEdit")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("EDIT");
            }
        }
    }
}